// var r = http.get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Obo74pWYA2dQ1gs992g9ufvr&client_secret=A0jCw00ZC2XfzZE8kplZGN7EhoOmqeDk");

// log("code = " + r.statusCode);
// log("html = " + r.body.string());
// log("html = " + r.body.access_token);


var myWord= {};
myWord.getAllword = function (){

    var img = getCaptureImg();
    var imgBase = images.toBase64(img);
    // log(imgBase.length)
    // var imgEncode = encodeURIComponent(imgBase);
    // log(imgEncode.length)
    var access_token = "24.73946d5ddd241007749743ae305e1111.2592000.1640607617.282335-25243556";
    
    var url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + access_token;
    var res = http.post(url, {
        "image": imgBase
    }, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    
    });
    var a = res.body.json();
    var result = a.words_result;
    return result;
}

myWord.clickWord = function (text, words, idx) {
    if(idx == null) {
        idx = 1;
    }
    for (var index = 0; index < words.length; index++) {
        var element = words[index];
        var word = element.words;
        if (word.indexOf(text) != -1 && idx > 0) {
            var location = element.location;
            var top = location.top;
            var left = location.left;
            var width = location.width;
            var height = location.height;
            var centerX = left + width / 2;
            var centerY = top + height / 2;
            click(centerX, centerY)
            break;
        }
        idx = idx -1;
    }
}





function prepareThings() {
    //请求截图
    threads.start(function () {
        sleep(1000)
        text("立即开始").findOne(3000).click();
    });
    if (!requestScreenCapture()) {
        toast("请求截图失败");
        exit();
    }
    sleep(500);
}


//获取截图
function getCaptureImg() {

    try {
        var img0 = captureScreen();
        sleep(100);
        if (img0 == null || typeof (img0) == "undifined") {
            myFunc.log("截图失败,脚本退出");
            exit();
        } else {
            return img0;
        }
    } catch (error) {
        prepareThings();
        getCaptureImg()
    }

}


module.exports = myWord;